package com.wwwarehouse.carddesk.fragment.mytabfragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.wwwarehouse.carddesk.R;
import com.wwwarehouse.carddesk.constant.CardDeskConstant;
import com.wwwarehouse.carddesk.eventbus_event.CardDeskMyEmailChangeEvent;
import com.wwwarehouse.common.activity.base.BaseFragment;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.common.custom_widget.ClearEditText;
import com.wwwarehouse.common.custom_widget.button.StateButton;
import com.wwwarehouse.common.http.NoHttpUtils;
import com.wwwarehouse.common.tools.CheckUtils;
import com.wwwarehouse.common.tools.LogUtils;
import com.wwwarehouse.common.tools.ToastUtils;
import com.wwwarehouse.common.tools.rsa.StringUtils;

import java.util.HashMap;
import java.util.Map;

import de.greenrobot.event.EventBus;

/**
 * author ：minifly
 * date: 2017/10/21
 * time: 10:52
 * desc: 信息页面修改邮箱
 */
public class MyMessageChangeEmailFragment extends BaseFragment {

    private View v;
    private final static int GET_MODIFY_EMAIL_INT = 10001;
    private ClearEditText mDeskMyEmailEdittext;
    private StateButton mDeskMyEmailStatebutton;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        v = inflater.inflate(R.layout.fragment_desk_my_message_changeemail, container, false);
        initView(v);
        return v;

    }

    public void initView(View view) {

        mDeskMyEmailEdittext = (ClearEditText) v.findViewById(R.id.desk_my_email_edittext);

        mDeskMyEmailStatebutton = (StateButton) v.findViewById(R.id.desk_my_email_statebutton);

        mDeskMyEmailEdittext.setOnMyFocusChangeListener(new ClearEditText.onMyFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {

            }
        });

        mDeskMyEmailEdittext.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (!StringUtils.isNullString(mDeskMyEmailEdittext.getText().toString())) {
                    mDeskMyEmailStatebutton.setEnabled(true);
                    mDeskMyEmailStatebutton.setText(getString(R.string.button_text_save));
                }else{
                    mDeskMyEmailStatebutton.setEnabled(false);
                    mDeskMyEmailStatebutton.setText(getString(R.string.confirm));
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });


        if(getArguments()!=null){
//            mDeskMyEmailEdittext.setText(getArguments().getString("email"));
        }

        mDeskMyEmailStatebutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if(CheckUtils.isEmail(mDeskMyEmailEdittext.getText().toString())){
                    //URL_MY_MODIFY_EMAIL
                    showProgressDialog();
                    Map<String, String> map = new HashMap<>();
                    map.put("email", "" + mDeskMyEmailEdittext.getText().toString());
                    NoHttpUtils.httpPost(CardDeskConstant.URL_MY_MODIFY_EMAIL, map, responseListener, GET_MODIFY_EMAIL_INT);
                    LogUtils.showErrLog("发送邮箱" + map.toString());
                }else{
                    ToastUtils.showToastAtTop("" + getString(R.string.taskdesk_my_message_email_error));
                }



            }
        });
    }

    @Override
    public void setTitle() {
        if (peekFragment() instanceof  MyMessageChangeEmailFragment) {
            mActivity.setTitle(getString(R.string.taskdesk_my_changeemail));
        }
    }

    @Override
    public void requestDatas() {

    }

    NoHttpUtils.OnResponseListener responseListener = new NoHttpUtils.OnResponseListener() {
        @Override
        public void onSucceed(CommonClass commonClass, int i) {
//            LogUtils.showErrLog("邮箱提交成功： " + commonClass.getData().toString());
            switch (i) {
                case GET_MODIFY_EMAIL_INT:
                    if("0".equals(commonClass.getCode())){//改动成功退出页面
                        popFragment();
                        EventBus.getDefault().post(new CardDeskMyEmailChangeEvent("" + mDeskMyEmailEdittext.getText().toString()));
                        ToastUtils.showToastAtTop("" + "邮箱修改成功");
                    }else{
                        ToastUtils.showToastAtTop("" + commonClass.getMsg());
                    }
                    break;
            }
        }

        @Override
        public void onFailed(String s, int i) {

        }

        @Override
        public void onFinish(int i) {
            dismissProgressDialog();
        }
    };


}
